package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.AppraiseCommodityModel;

/**
 * Created by wu on 2018/1/20.
 */

public class AppraiseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private AppraiseCommodityModel model;
    private List<AppraiseCommodityModel.commodityAppraise> list;
    private Context context;

    public AppraiseAdapter(AppraiseCommodityModel model) {
        this.model = model;
        this.list = model.getCommodity_appraise_list();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        switch (viewType) {
            case 0:
                return new AppraiseCommodityViewHolder(LayoutInflater.from(context).inflate(R.layout.item_appraise_commodity, parent, false));
            case 1:
                return new AppraiseStoreViewHolder(LayoutInflater.from(context).inflate(R.layout.item_appraise_store, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case 0:
                Glide.with(context).load(list.get(position).getCommodity_picture_url())
                        .dontTransform()
                        .placeholder(R.drawable.ic_def)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .into(((AppraiseCommodityViewHolder) holder).pic);
                ((AppraiseCommodityViewHolder) holder).state.setText(setState(list.get(position).getDescription_score()));
                break;
            case 1:
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position == list.size() ? 1 : 0;
    }

    @Override
    public int getItemCount() {
        return list == null ? 1 : list.size() + 1;
    }

    public class AppraiseCommodityViewHolder extends RecyclerView.ViewHolder {
        public ImageView pic;
        public top.drinyeit.weilong.view.RatingBar appraiseCommodity;
        public TextView state;
        public EditText appraise;

        public AppraiseCommodityViewHolder(View itemView) {
            super(itemView);
            pic = itemView.findViewById(R.id.iv_picture);
            appraiseCommodity = itemView.findViewById(R.id.rb_appraise_commodity);
            state = itemView.findViewById(R.id.tv_state);
            appraise = itemView.findViewById(R.id.et_appraise);

            appraiseCommodity.setOnRatingChangeListener(new top.drinyeit.weilong.view.RatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(float ratingCount) {
                    switch ((int) ratingCount) {
                        case 1:
                            state.setText(setState("1"));
                            list.get(getAdapterPosition()).setDescription_score("1");
                            break;
                        case 2:
                            state.setText(setState("2"));
                            list.get(getAdapterPosition()).setDescription_score("2");
                            break;
                        case 3:
                            state.setText(setState("3"));
                            list.get(getAdapterPosition()).setDescription_score("3");
                            break;
                        case 4:
                            state.setText(setState("4"));
                            list.get(getAdapterPosition()).setDescription_score("4");
                            break;
                        case 5:
                            state.setText(setState("5"));
                            list.get(getAdapterPosition()).setDescription_score("5");
                            break;
                    }
                }
            });
        }


    }

    public class AppraiseStoreViewHolder extends RecyclerView.ViewHolder {
        public top.drinyeit.weilong.view.RatingBar appraiseService, appraiseLogistics;

        public AppraiseStoreViewHolder(View itemView) {
            super(itemView);
            appraiseService = itemView.findViewById(R.id.rb_appraise_store_service);
            appraiseLogistics = itemView.findViewById(R.id.rb_appraise_store_logistics);

            appraiseService.setOnRatingChangeListener(new top.drinyeit.weilong.view.RatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(float ratingCount) {
                    switch ((int) ratingCount) {
                        case 1:
                            model.getStore_appraise().setService_score("1");
                            break;
                        case 2:
                            model.getStore_appraise().setService_score("2");
                            break;
                        case 3:
                            model.getStore_appraise().setService_score("3");
                            break;
                        case 4:
                            model.getStore_appraise().setService_score("4");
                            break;
                        case 5:
                            model.getStore_appraise().setService_score("5");
                            break;
                    }
                }
            });

            appraiseLogistics.setOnRatingChangeListener(new top.drinyeit.weilong.view.RatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(float ratingCount) {
                    switch ((int) ratingCount) {
                        case 1:
                            model.getStore_appraise().setLogistics_score("1");
                            break;
                        case 2:
                            model.getStore_appraise().setLogistics_score("2");
                            break;
                        case 3:
                            model.getStore_appraise().setLogistics_score("3");
                            break;
                        case 4:
                            model.getStore_appraise().setLogistics_score("4");
                            break;
                        case 5:
                            model.getStore_appraise().setLogistics_score("5");
                            break;
                    }
                }
            });
        }
    }

    private String setState(String score) {
        if (score == null){
            return "";
        }
        int score0 = 0;
        score0 = Integer.parseInt(score);

        switch (score0) {
            case 1:
                return "较差";
            case 2:
                return "一般";
            case 3:
                return "很好";
            case 4:
                return "满意";
            case 5:
                return "棒极了";
            default:
                return "";
        }
    }

    public AppraiseCommodityModel getData() {
        return model;
    }
}
